<?php

namespace base\common\model;

use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 有关时间的模型
 * Class TimeModel.
 */
class TimeModelBase extends BaseModel
{
    /**
     * 自动时间戳类型.
     * @var string
     */
    protected $autoWriteTimestamp = true;

    /**
     * 添加时间.
     * @var string
     */
    protected $createTime = 'create_time';

    /**
     * 更新时间.
     * @var string
     */
    protected $updateTime = 'update_time';

    /**
     * 软删除.
     */
    use SoftDelete;

    protected $deleteTime = 'delete_time';

    protected $defaultSoftDelete = 0;

    public static function timeAttrSet($value)
    {
        if (empty($value)) {
            return 0;
        }
        if (is_numeric($value)) {
            return $value;
        }

        $time = strtotime($value);
        if ($time === false) {
            return 0;
        }

        return $time;
    }

    public static function timeAttrGet($value, $format = 'Y-m-d H:i:s')
    {
        if (empty($value)) {
            return '';
        }
        if (is_numeric($value)) {
            return date($format, $value);
        }

        return $value;
    }
}
